package com.uam.web.common.utils;

import com.mapbox.geojson.Point;
import com.mapbox.turf.TurfConstants;
import com.mapbox.turf.TurfMeasurement;
import com.uam.utils.geo.PoiTypeEnums;
import com.uam.utils.geo.buffer.LineBufferImpl;
import io.swagger.models.auth.In;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;

import java.util.ArrayList;
import java.util.List;

/**
 * data core 代码提取
 *
 * @author Administrator
 */
public class KeepPointDateInfo {
    static LineBufferImpl lineBuffer = (LineBufferImpl) PoiTypeEnums.POLYLINE.getBufferUtil();

    public static void main(String[] args) {

//        Coordinate[] pointsAbc = new Coordinate[3];
//        pointsAbc[0] = new Coordinate(112.97360514514861, 28.25000115239046);
//        pointsAbc[1] = new Coordinate(112.97176586714133, 28.250675647155795);
//        pointsAbc[2] =new Coordinate(112.96992637413378, 28.250001582868546);
//
//        dataInfo(pointsAbc,30);
    }

    public static Geometry  intersection(Coordinate[] points1,Coordinate[] points2){
        LineString lineString1 = lineBuffer.geometryCoordinate(points1);
        LineString lineString2 = lineBuffer.geometryCoordinate(points2);
        return lineString1.intersection(lineString2);
    }

    public static List<Point> dataInfo(List<Coordinate> arr, Integer distance){

        Coordinate a0=arr.get(0);
        Coordinate a1=arr.get(1);
        Coordinate a2=arr.get(2);
        Point sModel = Point.fromLngLat(a0.x, a0.y);
        Point pModelTemp = Point.fromLngLat(a1.x, a1.y);

        //中间点
        double initAngle = TurfMeasurement.bearing(sModel, pModelTemp);
        Point pMode = TurfMeasurement.destination(pModelTemp, distance,  initAngle + 90.00D, TurfConstants.UNIT_METRES);

        //取开始点
        double initAngleStart = TurfMeasurement.bearing(pMode, sModel);
        Point startPoint = TurfMeasurement.destination(sModel, 30,   initAngleStart + 90.00D, TurfConstants.UNIT_METRES);


        Coordinate[] pointsAbc = new Coordinate[2];
        pointsAbc[0] = a0;
        pointsAbc[1] = a1;


        Coordinate[] pointsStart = new Coordinate[2];
        pointsStart[0] = new Coordinate(startPoint.coordinates().get(0), startPoint.coordinates().get(1));
        pointsStart[1] = new Coordinate(pMode.coordinates().get(0), pMode.coordinates().get(1));


        Geometry geometry = intersection(pointsAbc,pointsStart);
        if (geometry != null && !geometry.isEmpty()) {
            startPoint = TurfMeasurement.destination(sModel, distance, initAngleStart - 90.00D, TurfConstants.UNIT_METRES);
        }

        //取结束点
        Point eModel = Point.fromLngLat(a2.x, a2.y);
        double initAngleEnd = TurfMeasurement.bearing(pMode, eModel);
        Point endPoint = TurfMeasurement.destination(eModel, distance, initAngleEnd + 90.00D, TurfConstants.UNIT_METRES);

        Coordinate[] pointsENd = new Coordinate[2];
        pointsENd[0] = a1;
        pointsENd[1] = a2;

        Coordinate[] pointsMode = new Coordinate[2];
        pointsMode[0] = new Coordinate(pMode.coordinates().get(0), pMode.coordinates().get(1));
        pointsMode[1] = new Coordinate(endPoint.coordinates().get(0), endPoint.coordinates().get(1));

        geometry = intersection(pointsENd,pointsMode);
        if (geometry != null && !geometry.isEmpty()) {
            endPoint = TurfMeasurement.destination(eModel, distance, initAngleStart - 90.00D, TurfConstants.UNIT_METRES);
        }

       List<Point> points=new ArrayList<>();
        points.add(startPoint);
        points.add(pMode);
        points.add(endPoint);
        System.out.println(" 计算新点--  "+startPoint.coordinates().get(0) + "," + startPoint.coordinates().get(1) + ";" + pMode.coordinates().get(0) + "," + pMode.coordinates().get(1) + ";" + endPoint.coordinates().get(0) + "," + endPoint.coordinates().get(1));
        return points;
    }
}
